Return to start page

Core/Interface/Struct Multiboard Bar.j

Code

		
1			library AStructCoreInterfaceMultiboardBar requires optional ALibraryCoreDebugMisc, AStructCoreGeneralHashTable
2
3 /**
4 * @todo vJass bug, should be a part of @struct AMultiboardBar.
5 * This represents the function which controls both MultiboardBar values:
6 * Value and maximum value.
7 */
8 function interface AMultiboardBarValueFunction takes AMultiboardBar multiboardBar returns real
9
10 struct AMultiboardBar
11 //static constant members
12 private static constant integer maxLength = 20
13 //start members
14 private multiboard m_multiboard
15 private integer m_column
16 private integer m_row
17 private integer m_length
18 private real m_refreshRate
19 private boolean m_horizontal
20 //dynamic members
21 private real m_value
22 private real m_maxValue
23 private string array m_valueIcon[AMultiboardBar.maxLength]
24 private string array m_emptyIcon[AMultiboardBar.maxLength]
25 private AMultiboardBarValueFunction m_valueFunction
26 private AMultiboardBarValueFunction m_maxValueFunction
27 //members
28 private integer m_colouredPart
29 private trigger m_refreshTrigger
30
31 //! runtextmacro optional A_STRUCT_DEBUG("\"AMultiboardBar\"")
32
33 //dynamic member methods
34
35 public method setValue takes real value returns nothing
36 set this.m_value = value
37 endmethod
38
39 public method value takes nothing returns real
40 return this.m_value
41 endmethod
42
43 public method setMaxValue takes real maxValue returns nothing
44 set this.m_maxValue = maxValue
45 endmethod
46
47 public method maxValue takes nothing returns real
48 return this.m_maxValue
49 endmethod
50
51 public method setValueIcon takes integer length, string valueIcon returns nothing
52 set this.m_valueIcon[length] = valueIcon
53 endmethod
54
55 public method valueIcon takes integer length returns string
56 return this.m_valueIcon[length]
57 endmethod
58
59 public method setEmptyIcon takes integer length, string emptyIcon returns nothing
60 set this.m_emptyIcon[length] = emptyIcon
61 endmethod
62
63 public method emptyIcon takes integer length returns string
64 return this.m_emptyIcon[length]
65 endmethod
66
67 /// Sets the function which should return the value of the multiboard bar when it refreshs.
68 public method setValueFunction takes AMultiboardBarValueFunction valueFunction returns nothing
69 set this.m_valueFunction = valueFunction
70 endmethod
71
72 public method valueFunction takes nothing returns AMultiboardBarValueFunction
73 return this.m_valueFunction
74 endmethod
75
76 public method setMaxValueFunction takes AMultiboardBarValueFunction maxValueFunction returns nothing
77 set this.m_maxValueFunction = maxValueFunction
78 endmethod
79
80 public method maxValueFunction takes nothing returns AMultiboardBarValueFunction
81 return this.m_maxValueFunction
82 endmethod
83
84 //methods
85
86 /// Die Farbe des Feldes mit Wert wird je nach Anteil des Wertes vom Maximalwert gesetzt.
87 public method refresh takes nothing returns nothing
88 local integer i
89 local multiboarditem multiboardItem
90 set this.m_colouredPart = R2I(this.m_value * I2R(this.m_length) / this.m_maxValue)
91 set i = 0
92 loop
93 exitwhen (i == this.m_length)
94 if (this.m_horizontal) then
95 set multiboardItem = MultiboardGetItem(this.m_multiboard, this.m_row, this.m_column + i)
96 else
97 set multiboardItem = MultiboardGetItem(this.m_multiboard, this.m_row + i, this.m_column)
98 endif
99 //coloured part
100 if (i < this.m_colouredPart) then
101 call MultiboardSetItemIcon(multiboardItem, this.m_valueIcon[i])
102 //plain Part
103 else
104 call MultiboardSetItemIcon(multiboardItem, this.m_emptyIcon[i])
105 endif
106 call MultiboardReleaseItem(multiboardItem) //TEST
107 set multiboardItem = null
108 set i = i + 1
109 endloop
110 endmethod
111
112 /// First call after setting the length.
113 public method setIcons takes integer start, integer end, string icon, boolean valueIcon returns nothing
114 local integer i
115 debug if ((start >= 0) and (start < this.m_length)) then
116 debug if ((end > 0) and (end < this.m_length)) then
117 set i = start
118 loop
119 exitwhen(i == end + 1)
120 if (valueIcon) then
121 set this.m_valueIcon[i] = icon
122 else
123 set this.m_emptyIcon[i] = icon
124 endif
125 set i = i + 1
126 endloop
127 debug else
128 debug call this.print("The value 'end' has an invalid size: " + I2S(end) + ".")
129 debug endif
130 debug else
131 debug call this.print("The value 'start' has an invalid size: " + I2S(start) + ".")
132 debug endif
133 endmethod
134
135 //comfort methods
136
137 /// Erst aufrufen, nachdem man die Länge gesetzt hat.
138 public method setAllIcons takes string icon, boolean valueIcon returns nothing
139 call this.setIcons(0, this.m_length - 1, icon, valueIcon)
140 endmethod
141
142 /// @return The index of the first field (column or row) which is not used by the bar (alignment is left to right and up to bottom).
143 public method firstFreeField takes nothing returns integer
144 if (this.m_horizontal) then
145 return this.m_column + this.m_length
146 endif
147 return this.m_row + this.m_length
148 endmethod
149
150 private method resizeMultiboard takes nothing returns nothing
151 if (this.m_horizontal) then
152 if (MultiboardGetColumnCount(this.m_multiboard) < (this.m_column + this.m_length)) then
153 call MultiboardSetColumnCount(this.m_multiboard, this.m_column + this.m_length)
154 endif
155 if (MultiboardGetRowCount(this.m_multiboard) <= this.m_row) then
156 call MultiboardSetRowCount(this.m_multiboard, this.m_row + 1)
157 endif
158 else
159 if (MultiboardGetColumnCount(this.m_multiboard) <= this.m_column) then
160 call MultiboardSetColumnCount(this.m_multiboard, this.m_column + 1)
161 endif
162 if (MultiboardGetRowCount(this.m_multiboard) < (this.m_row + this.m_length)) then
163 call MultiboardSetRowCount(this.m_multiboard, (this.m_row + this.m_length))
164 endif
165 endif
166 endmethod
167
168 private method setupMultiboardItems takes nothing returns nothing
169 local integer i
170 local multiboarditem multiboardItem
171 set i = 0
172 loop
173 exitwhen (i == this.m_length)
174 if (this.m_horizontal) then
175 set multiboardItem = MultiboardGetItem(this.m_multiboard, this.m_row, this.m_column + i)
176 else
177 set multiboardItem = MultiboardGetItem(this.m_multiboard, this.m_row + i, this.m_column)
178 endif
179 call MultiboardSetItemStyle(multiboardItem, false, true)
180 call MultiboardSetItemWidth(multiboardItem, 0.01) //Einzeln einstellen, um nicht das ganze Multiboard zu verändern
181 call MultiboardReleaseItem(multiboardItem)
182 set multiboardItem = null
183 set i = i + 1
184 endloop
185 endmethod
186
187 private static method triggerActionRefresh takes nothing returns nothing
188 local trigger triggeringTrigger = GetTriggeringTrigger()
189 local AMultiboardBar this = AHashTable.global().handleInteger(triggeringTrigger, "this")
190 set this.m_value = this.m_valueFunction.evaluate(this)
191 set this.m_maxValue = this.m_maxValueFunction.evaluate(this)
192 call this.refresh()
193 set triggeringTrigger = null
194 endmethod
195
196 private method createRefreshTrigger takes nothing returns nothing
197 local event triggerEvent
198 local triggeraction triggerAction
199 if (this.m_refreshRate > 0.0) then
200 set this.m_refreshTrigger = CreateTrigger()
201 set triggerEvent = TriggerRegisterTimerEvent(this.m_refreshTrigger, this.m_refreshRate, true)
202 set triggerAction = TriggerAddAction(this.m_refreshTrigger, function AMultiboardBar.triggerActionRefresh)
203 call AHashTable.global().setHandleInteger(this.m_refreshTrigger, "this", this)
204 set triggerEvent = null
205 set triggerAction = null
206 endif
207 endmethod
208
209 /**
210 * If there aren't enough items in multiboard yet required onces will be added automatically.
211 * @param refreshRate If this value is bigger than 0 multiboard bar will be refreshed.
212 * @param horizontal This value is not dynamic.
213 */
214 public static method create takes multiboard usedMultiboard, integer column, integer row, integer length, real refreshRate, boolean horizontal, real value, real maxValue, AMultiboardBarValueFunction valueFunction, AMultiboardBarValueFunction maxValueFunction returns thistype
215 local thistype this = thistype.allocate()
216 //start members
217 set this.m_multiboard = usedMultiboard
218 set this.m_column = column
219 set this.m_row = row
220 set this.m_length = length
221 set this.m_refreshRate = refreshRate
222 set this.m_horizontal = horizontal
223 //dynamic members
224 set this.m_value = value
225 set this.m_maxValue = maxValue
226 set this.m_valueFunction = valueFunction
227 set this.m_maxValueFunction = maxValueFunction
228 //members
229 set this.m_colouredPart = 0
230
231 call this.resizeMultiboard()
232 call this.setupMultiboardItems()
233 call this.createRefreshTrigger()
234 return this
235 endmethod
236
237 private method destroyRefreshTrigger takes nothing returns nothing
238 if (this.m_refreshRate > 0.0) then
239 call AHashTable.global().destroyTrigger(this.m_refreshTrigger)
240 set this.m_refreshTrigger = null
241 endif
242 endmethod
243
244 public method onDestroy takes nothing returns nothing
245 //start members
246 set this.m_multiboard = null
247
248 call this.destroyRefreshTrigger()
249 endmethod
250 endstruct
251
252 endlibrary